package com.app;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.app.bean.ClassmateDetailBean;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

public class ClassmateDetailActivity extends AppCompatActivity {


    private TextView idStr;
    private TextView nameStr;
    private TextView classStr;
    private TextView phoneStr;
    private TextView emailStr;
    private String id;
    private Button fanhui;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_classmate_detail);
//        Intent intent = new Intent();
//        String id = intent.getStringExtra("id");
        id = getIntent().getStringExtra("id");
        Toast.makeText(ClassmateDetailActivity.this, id, Toast.LENGTH_SHORT).show();
        initView();
        initData();
        initEvent();
    }

    private void initView() {
        idStr = findViewById(R.id.std_info_id_tv);
        nameStr = findViewById(R.id.std_info_name_tv);
        classStr = findViewById(R.id.std_info_class_tv);
        phoneStr = findViewById(R.id.std_info_phone_tv);
        emailStr = findViewById(R.id.std_info_email_tv);
        fanhui = findViewById(R.id.fanhui);
    }

    private void initData() {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("id",id);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestQueue requestQueue = Volley.newRequestQueue(ClassmateDetailActivity.this);
        String url = "https://www.xhjjyst.com/freshmenapp/classmate/findone";
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Gson gson = new Gson();
                ClassmateDetailBean classmateDetailBean = gson.fromJson(jsonObject.toString(), ClassmateDetailBean.class);
                idStr.setText(classmateDetailBean.getData().getId()+"");
                nameStr.setText(classmateDetailBean.getData().getName()+"");
                classStr.setText(classmateDetailBean.getData().getClassName()+"");
                phoneStr.setText(classmateDetailBean.getData().getTelephone()+"");
                emailStr.setText(classmateDetailBean.getData().getEmail()+"");

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(ClassmateDetailActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
            }
        });
        requestQueue.add(request);
    }
   private void initEvent(){
        fanhui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(ClassmateDetailActivity.this,ClassmateListActivity.class));
            }
        });
   }
}